package com.jdrx.sw.sewage.service;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.HandleIdeaDTO;
import com.jdrx.sw.sewage.beans.entity.AlarmHandlePO;
import com.jdrx.sw.sewage.beans.entity.AlarmPO;
import com.jdrx.sw.sewage.common.enums.EAlarmHandleType;
import com.jdrx.sw.sewage.common.enums.EAlarmStatus;
import com.jdrx.sw.sewage.dao.AlarmDAO;
import com.jdrx.sw.sewage.dao.AlarmHandleDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class AlarmHandleServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(AlarmHandleServiceImpl.class);

    @Autowired
    private AlarmHandleDAO alarmHandleDAO;
    @Autowired
    private AlarmDAO alarmDAO;

    @Transactional(rollbackFor = Exception.class)
    public void handleAlarmSysNews(String handleIdea,
                                   Long alarmId,
                                   Integer handleType,
                                   String handleAttach,
                                   String handleUserName,
                                   String dataNode) {
        AlarmHandlePO alarmHandlePO = new AlarmHandlePO();
        alarmHandlePO.setHandleTime(LocalDateTime.now());
        alarmHandlePO.setHandleIdea(handleIdea);
        alarmHandlePO.setAlarmId(alarmId);
        alarmHandlePO.setHandleType(handleType);
        alarmHandlePO.setHandleAttach(handleAttach);
        alarmHandlePO.setHandleUserName(handleUserName);
        alarmHandlePO.setDataNode(dataNode);
        alarmHandleDAO.insert(alarmHandlePO);
    }

    public ResponseVO handleIdea(HandleIdeaDTO handleIdeaDTO, String dataNode) {
        if (null == handleIdeaDTO.getAlarmId()) return ResponseFactory2.err("告警id不能为空");

        AlarmPO alarmPO = alarmDAO.selectAlarmById(handleIdeaDTO.getAlarmId());
        if (null == alarmPO) return ResponseFactory2.err("告警不存在");

        if (EAlarmStatus.COMPLETED.getKey().equals(alarmPO.getAlarmStatus()))
            return ResponseFactory2.err("告警已关闭,不能提交意见");

        this.handleAlarmSysNews(handleIdeaDTO.getHandleIdea(),
                handleIdeaDTO.getAlarmId(),
                EAlarmHandleType.DUTY_USER_HANDLE.getKey(),
                handleIdeaDTO.getHandleAttach(),
                handleIdeaDTO.getUserName(), dataNode);

        return ResponseFactory2.ok();
    }
}
